if (!com) var com = {}
if (!com.logicpartners) com.logicpartners = {}
if (!com.logicpartners.designerTools) com.logicpartners.designerTools = {}

com.logicpartners.designerTools.qrcode = function () {
  var self = this
  this.counter = 1
  this.button = $('<div></div>')
    .addClass('designerToolbarQrcode designerToolbarButton')
    .attr('title', 'QRCode')
    .append($('<div></div>'))
  this.object = function (x, y, width, height) {
    this.toolName = 'qrcode'
    this.props = {
      type: { label: '类型', default: '二维码' },
      name: { label: '元素', default: 'QRCode ' + self.counter++ },
      contentSet: {
        label: '内容设置',
        default: 'fixedContent',
        options: [
          { label: '固定内容', value: 'fixedContent' },
          { label: '数据来源', value: 'dataSource' },
        ],
      },
      text: { label: '内容', default: 'Textbox' },
      dataSourcefield: { label: '来源字段', default: 'name', hidden: true },
      style: {
        label: '样式',
        default: 'QRCode',
        options: [
          { label: 'QRCode', value: 'QRCode' },
          { label: 'DataMatrix', value: 'DataMatrix' },
        ],
      },
      x: { label: '起始位置x(mm)', default: x },
      y: { label: '起始位置y(mm)', default: y },
      width: { label: '宽度(mm)', default: 12 },
      height: { label: '高度(mm)', default: 12 },
      ZindexTop: { label: '置顶', button: '置顶' },
      ZindexBottom: { label: '置底', button: '置底' },
      ZindexUp: { label: '上移一层', button: '上移一层' },
      ZindexDown: { label: '下移一层', button: '下移一层' },
    }
    for (var prop in this.props) {
      this[prop] = this.props[prop].default
    }
    this.readonly = ['type', 'name', 'height', 'dataSourcefield']

    this.setWidth = function (width) {
      if (this.readonly && this.readonly.includes('width')) return
      this.width = precisionNumber(width)
    }

    this.getWidth = function () {
      return this.width
    }

    this.setHeight = function (height) {
      if (this.readonly && this.readonly.includes('height')) return
      this.height = precisionNumber(height)
    }

    this.getHeight = function () {
      return this.height
    }

    this.hitTest = function (coords) {
      return (
        coords.x >= this.x && coords.x <= this.x + this.width && coords.y >= this.y && coords.y <= this.y + this.height
      )
    }

    this.draw = function (context, scale) {
      this.x = precisionNumber(this.x)
      this.y = precisionNumber(this.y)
      this.setWidth(this.width)
      this.height = this.width
      if (this.style == 'QRCode') {
        var tmpCanvas = $('<div></div>')[0]
        var qrcode = new QRCode(tmpCanvas, {
          text: this.text,
          width: this.width * scale,
          height: this.width * scale,
          colorDark: '#000000',
          colorLight: '#ffffff',
          correctLevel: 0,
        })
        var img = $(tmpCanvas).children('canvas')[0]
        context.drawImage(img, this.x, this.y, this.width, this.height)
        tmpCanvas.remove()
      } else if (this.style == 'DataMatrix') {
        var tmpCanvas = $('<canvas></canvas>')[0]
        var tmpctx = tmpCanvas.getContext('2d')
        var svgNode = DATAMatrix({
          msg: this.text,
          dim: this.width * scale,
          pad: 0,
          pal: ['#000000', '#ffffff'],
        })
        v = canvg.Canvg.fromString(tmpctx, svgNode.outerHTML)
        v.start()

        context.drawImage(tmpCanvas, this.x, this.y, this.width, this.height)
        tmpCanvas.remove()
      }
    }

    this.drawActive = function (context) {
      context.dashedStroke(this.x, this.y, this.x + this.width, this.y + this.height, [1, 0.5])
    }

    this.toZPL = function (labelwidth, labelheight) {
      if (this.contentSet == 'fixedContent') {
        if (this.style == 'QRCode') {
          var c = precisionNumber((this.width / 21) * 8, 0) //HARDCODE mybe related with dpi
          return `^FO${this.x},${this.y}^MUd^BQN,2,${c}^FDQA,${this.text}^FS^MUm`
        } else if (this.style == 'DataMatrix') {
          var c = precisionNumber((this.width / 15) * 8, 0) //HARDCODE mybe related with dpi
          return `^FO${this.x},${this.y}^BXN,${c},200^FD${this.text}^FS`
        }
      } else {
        if (this.style == 'QRCode') {
          var c = precisionNumber((this.width / 21) * 8, 0) //HARDCODE mybe related with dpi
          return `^FO${this.x},${this.y}^MUd^BQN,2,${c}^FDQA,{${this.dataSourcefield}}^FS^MUm`
        } else if (this.style == 'DataMatrix') {
          var c = precisionNumber((this.width / 15) * 8, 0) //HARDCODE mybe related with dpi
          return `^FO${this.x},${this.y}^BXN,${c},200^FD{${this.dataSourcefield}}^FS`
        }
      }
    }
  }
}
